/*
 * @Description: 注册界面
 * @Author: YWJ 
 * @Date: 2022-04-24 15:39:58 
 * @Last Modified by: YWJ
 * @Last Modified time: 2022-05-17 11:27:12
 */

<template>
  <div id="register">
    <!--<video preload="auto" class="me-video-player" autoplay="autoplay" loop="loop">
          <source src="../../static/vedio/sea.mp4" type="video/mp4">
      </video>-->

    <div class="me-login-box me-login-box-radius">
      <h1>Picture Share 注册</h1>

      <el-form ref="userForm" :model="userForm" :rules="rules">
        <el-form-item prop="nickname">
          <el-input placeholder="昵称" v-model="userForm.nickname"></el-input>
        </el-form-item>
        <el-form-item prop="account">
          <el-input placeholder="账号" v-model="userForm.account"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            placeholder="密码"
            type="password"
            v-model="userForm.password"
          ></el-input>
        </el-form-item>

        <el-form-item size="small" class="me-login-button">
          <el-button type="primary" @click.native.prevent="register('userForm')"
            >注册</el-button
          >
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { register, login } from "@/api/login.js";
export default {
  data() {
    return {
      userForm: {
        account: "",
        nickname: "",
        password: "",
        sculpture: "",
      },
      loginForm:{
        username:"",
        password:""
      },
      rules: {
        account: [
          { required: true, message: "请输入账号", trigger: "blur" },
          { max: 10, message: "不能大于10个字符", trigger: "blur" },
        ],
        nickname: [
          { required: true, message: "请输入昵称", trigger: "blur" },
          { max: 10, message: "不能大于10个字符", trigger: "blur" },
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          { max: 10, message: "不能大于10个字符", trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    register(formName) {
      let that = this;
      this.$refs[formName].validate((valid) => {
        if (valid) {
          //注册逻辑
          //1 请求后端接口进行注册
          //2 注册完成后 返回一个token令牌
          //3 前端进行token存储 并且其他组件也能知道
          register(that.userForm).then((res) => {
            var vo = res.data;
            if (vo.code == 10000) {
              this.loginForm.username = this.userForm.account;
              this.loginForm.password = this.userForm.password
              that.$message({
                message: "注册成功！",
                type: "success",
                showClose: true,
              });
              login(this.loginForm).then((res) => {
                var vo = res.data;
                if (vo.code == 10000) {
                  console.log(res.data);
                  localStorage.setItem("token", res.data.msg);
                  localStorage.setItem("account", res.data.data.account);
                  localStorage.setItem("uid", res.data.data.uid);
                  localStorage.setItem("nickname", res.data.data.nickname);
                  localStorage.setItem("state", res.data.data.userState);
                  localStorage.setItem("password", res.data.data.password);
                  localStorage.setItem("createTime", res.data.data.createTime);
                  localStorage.setItem("avatar", res.data.data.sculpture);
                  global.account = res.data.data.account;
                  that.$message({
                    message: "登陆成功！",
                    type: "success",
                    showClose: true,
                  });
                  this.$router.push({ path: `/` });
                  console.log(res.data.data);
                  console.log(localStorage.getItem("account"));
                } else if (vo.code == 10001) {
                  that.$message({
                    message: vo.msg,
                    type: "warning",
                    showClose: true,
                  });
                }
              });
              
            } else {
              that.$message({ message: vo.msg, type: "warn", showClose: true });
            }
          });
        } else {
          return false;
        }
      });
    },
  },
};
</script>

<style>
#login {
  min-width: 100%;
  min-height: 100%;
}

.me-video-player {
  background-color: transparent;
  width: 100%;
  height: 100%;
  object-fit: fill;
  display: block;
  position: absolute;
  left: 0;
  z-index: 0;
  top: 0;
}

.me-login-box {
  position: absolute;
  width: 300px;
  height: 320px;
  background-color: white;
  margin-top: 150px;
  margin-left: -180px;
  left: 50%;
  padding: 30px;
}

.me-login-box-radius {
  border-radius: 10px;
  box-shadow: 0px 0px 1px 1px rgba(161, 159, 159, 0.1);
}

.me-login-box h1 {
  text-align: center;
  font-size: 24px;
  margin-bottom: 20px;
  vertical-align: middle;
}

.me-login-design {
  text-align: center;
  font-family: "Open Sans", sans-serif;
  font-size: 18px;
}

.me-login-design-color {
  color: #5fb878 !important;
}

.me-login-button {
  text-align: center;
}

.me-login-button button {
  width: 100%;
}
</style>